/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { message, Image } from 'antd';
import type { ActionType } from '@ant-design/pro-table';
import { ShopListItems } from '../../..';
import { takeaway_platform } from '../../../utils/index.config';
import { ModalForm, ProFormText, ProFormSelect } from '@ant-design/pro-form';
import { bindKeruyun } from '@/services/jdk/register';
import styles from './index.less';

interface BindTakeawayModalProps {
  children: JSX.Element;
  tableRef: ActionType | any;
  item?: ShopListItems | any;
}

const KbBindTakeawayModal: React.FC<BindTakeawayModalProps> = ({ item, children, tableRef }) => {
  const { id: shop_id = '' } = item || {};
  return (
    <ModalForm
      className={styles.takeawayModal}
      title='绑定外卖平台'
      trigger={children}
      width={500}
      labelCol={{ span: 6 }}
      layout='horizontal'
      modalProps={{
        destroyOnClose: true,
      }}
      onFinish={async (values) => {
        const { code, msg } = await bindKeruyun({
          shop_id,
          ...values,
        });
        if (code == 0) {
          message.success('操作成功');
          tableRef.current?.reload();
          return true;
        } else {
          message.error(msg);
          return false;
        }
      }}
    >
      <ProFormSelect
        label='选择外卖平台'
        width='sm'
        fieldProps={{ value: 'kry' }}
        valueEnum={takeaway_platform}
        placeholder='请选择外卖平台'
      />
      <ProFormText
        label='门店id'
        name='source_shop_id'
        width='xl'
        placeholder='输入id'
        rules={[{ required: true, message: '请输入id' }]}
      />
      <ProFormText
        label='token'
        name='token'
        width='xl'
        placeholder='输入token'
        rules={[{ required: true, message: '请输入token' }]}
      />
      <ProFormText
        label=' '
        width='xl'
        readonly
        colon={false}
        fieldProps={{ value: ' ' }}
        addonBefore={[
          <div>
            登录我方客如云平台账户，查看门店id、token，<a>查看示例&nbsp;</a>
            <Image
              src='https://osscdn-kbad.kuaidihelp.com/admin/ad/2023/05/06/645619575ed5e/tips.png'
              alt='查看示例'
              placeholder
              width={30}
            />
          </div>,
        ]}
      />
    </ModalForm>
  );
};

export default KbBindTakeawayModal;
